package com.mbmall.product.service.impl;

import com.mbmall.product.service.CategoryBrandRelationService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.utils.PageUtils;
import com.common.utils.Query;

import com.mbmall.product.dao.CategoryDao;
import com.mbmall.product.entity.CategoryEntity;
import com.mbmall.product.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    CategoryBrandRelationService categoryRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        List<CategoryEntity> entities = baseMapper.selectList(null);
        //查询一级分类
        List<CategoryEntity> leve1Menus=entities.stream().filter((categoryEntity)->{
            return categoryEntity.getParentCid()==0;
        }).map((menu)->{
            menu.setChildren(getChildrens(menu,entities));
            return menu;
        }).sorted((menu1,menu2)->{
            return (menu1.getSort()==null?0:menu1.getSort())-(menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());

        return leve1Menus;
    }

    @Override
    public void removeMenusbyIds(List<Long> asList) {
        // TODO 检查当前删除菜单是否被引用
        baseMapper.deleteBatchIds(asList);
    }

    @Override
    public Long[] findCategoryPath(Long categoryId) {
        List<Long> paths = new ArrayList<Long>();
        List<Long> parentPath=findParentPath(categoryId,paths);
        Collections.reverse(parentPath);
        return parentPath.toArray(new Long[parentPath.size()]);
    }

    /***
     * 级联更新所有的关联数据
     * @param category
     */
    @Override
    public void updateCascade(CategoryEntity category) {

        this.updateById(category);
        categoryRelationService.updateCategory(category.getCatId(), category.getName());

    }

    //递归查找父id
    private List<Long> findParentPath(Long categoryId,List<Long> paths){
        paths.add(categoryId);
        CategoryEntity category=this.getById(categoryId);
        if(category.getParentCid()!=0){
            findParentPath(category.getParentCid(),paths);
        }
        return paths;
    }

    //递归查找所有菜单的子菜单
    private List<CategoryEntity> getChildrens(CategoryEntity eRoot,List<CategoryEntity> all) {
        List<CategoryEntity> children=all.stream().filter((categoryEntity)->{
            return categoryEntity.getParentCid().equals(eRoot.getCatId());
        }).map((categoryEntity)->{
            //找到子菜单
            categoryEntity.setChildren(getChildrens(categoryEntity,all));
            return categoryEntity;
        }).sorted((menu1,menu2)->{
            return (menu1.getSort()==null?0:menu1.getSort())-(menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());
        return children;
    }

}